// 定义状态接口
interface State {
    void doAction(Context context);
    State getNextState();
}

// 定义上下文类，用于保存状态机的状态
class Context {
    private State state;

    public Context(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public void request(State state) {
        setState(state);
    }
}

// 定义具体的状态类
class StateA implements State {
    public void doAction(Context context) {
        System.out.println("Doing action in State A");
    }

    public State getNextState() {
        return new StateB();
    }
}

class StateB implements State {
    public void doAction(Context context) {
        System.out.println("Doing action in State B");
    }

    public State getNextState() {
        return new StateA();
    }
}

// 状态机类
class StateMachine {
    private Context context;

    public StateMachine(Context context) {
        this.context = context;
    }

    public void start() {
        while (true) {
            State currentState = context.getState();
            currentState.doAction(context);
            context.request(currentState.getNextState());
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Context context = new Context(new StateA());
        StateMachine stateMachine = new StateMachine(context);
        new Thread(stateMachine::start).start();
    }
}